﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

using FLEN_Sim.containerobjects;

namespace FLEN_Sim.funct
{
    /// <summary>
    /// Stellt Methoden zum Serialisieren und Deserialisieren von IFLENObjects bereit
    /// </summary>
    /// <typeparam name="T">Implementierung des IFLENObject-Interfaces</typeparam>
    public class FLEN_Serializer<T> where T : IFLENObject
    {
        private static List<T> list;

        private FLEN_Serializer(List<T> List)
        {}

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("List", list);            
        }

        public static bool serialize(List<T> List, string file)
        {
            list = List;

            if (list != null)
            {
                Stream stream = File.Open(file, FileMode.Create);
                BinaryFormatter bformatter = new BinaryFormatter();

                if (stream != null)
                {
                    bformatter.Serialize(stream, list);
                    stream.Close();

                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public static object deserialize(string file)
        {
            Stream stream = null;
            BinaryFormatter bformatter = new BinaryFormatter();

            stream = File.Open(file, FileMode.Open);

            return bformatter.Deserialize(stream);
        }
    }
}
